<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { Lock } from '@element-plus/icons-vue'
// do not use same name with ref
const form = ref({
  oldPassword: '',
  newPassword: '',
  reNewPassword: '',
})
//校验密码
const checkRePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次确认密码'))
  } else if (value !== form.value.newPassword) {
    callback(new Error('两次输入的密码不一样'))
  } else {
    callback()
  }
}
const rules = {
  oldPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, max: 16, message: '长度为8-16位非空字符', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, max: 16, message: '长度为8-16位非空字符', trigger: 'blur' }
  ],
  reNewPassword: [
    { validator: checkRePassword, trigger: 'blur' },
  ],
}
import { ElMessage, ElMessageBox } from 'element-plus';
import { updatePasswordService } from '@/api/user';
const onSubmit = ()=> {
  ElMessageBox.confirm(
        '您确认要修改吗?',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            let result = await updatePasswordService(form.value);
            ElMessage({
                type: 'success',
                message: '修改成功',
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '放弃修改',
            })
        })
}
</script>
<template>
  <el-form :model="form" label-width="auto" style="max-width: 600px" :rules="rules">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input :prefix-icon="Lock" v-model="form.oldPassword" />
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input :prefix-icon="Lock" v-model="form.newPassword" />
    </el-form-item>
    <el-form-item label="再次输入新密码" prop="reNewPassword">
      <el-input :prefix-icon="Lock" v-model="form.reNewPassword" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">确认</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>
